package cn.jixie;

import cn.jixie.MsgDispatcher.TextDispatcher;
import cn.jixie.utils.CheckConnectUtils;
import cn.jixie.utils.MessageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@Controller
public class teset {

    /**
     * 进行微信用户验证,只能是Get方法
     * @param request
     * @param response
     */
    @RequestMapping(value = "/wechat" ,method = RequestMethod.GET)
    public void connectValidate(HttpServletRequest request , HttpServletResponse response) throws IOException {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        System.out.println(""+signature +"@"+timestamp +"$"+nonce +"^"+echostr);
        PrintWriter out = response.getWriter();
        out.print(echostr);
//        if(CheckConnectUtils.checkConnectWithWechat(signature,timestamp,nonce)){
//            out.print(echostr);
//        }else {
//            System.out.println("jaba");
//        }
    }
    /**
     * 客户端进行的消息处理
     * @param request
     * @param response
     */
    @RequestMapping(value = "/wechat.do" ,method = RequestMethod.POST)
    public void disposeClientMessage(HttpServletRequest request , HttpServletResponse response ) throws IOException {
        backTestFunction(request , response );
    }

    /**
     * 文字回复功能开发
     * @param request
     * @param response
     * @throws Exception
     * @throws IOException
     */
    public void backTestFunction(HttpServletRequest request , HttpServletResponse response ) throws IOException {
        //防止进行post提交和响应的消息乱码
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try{
            //将发送过来的消息XML形式转为map内容
            Map<String , String> map = MessageUtils.xmlToMap(request);
            String fromUserName = map.get("FromUserName");
            String toUserName = map.get("ToUserName");
            String msgType = map.get("MsgType");
            String content = map.get("Content");
            String message = null ;
            if(MessageUtils.MESSAGE_TEXT.equals(msgType)){
                //进行关键字回复功能
                if("1".equals(content)){
                    message = TextDispatcher.processMessage(map);
                }else if("2".equals(content)){
                   // message = TextDispatcher.processMessage(request);
                }
                else if("3".equals(content)){
                    //客户端输入“3”，返回一条图文消息

                }else if("4".equals(content)){
                    //客户端输入“4”，返回一条图片消息

                }else if("5".equals(content)){
                    //客户端输入“5”，返回一首音乐消息

                }else if("6".equals(content)){
                    //测试是否能够获取用户的信息

                }else if(content.startsWith("翻译")){
                    //客户端输入“以翻译开头”，返回对应的翻译信息
                    /*String translateResult = TranslationUtils.translate(content.substring(2,content.length()));
                    message = MessageUtils.initText(fromUserName,toUserName , translateResult);*/
                }else {
                    message = MessageUtils.initText(fromUserName,toUserName,"success" );
                }
            }
            //打印输出的xml格式内容，方便进行调试
            System.out.println(message);
            out.print(message);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.close();
        }
    }



}
